package com.example.shiwuyouproject.ui.fragment.my.activity

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Color
import android.graphics.Typeface
import android.os.Bundle
import android.widget.LinearLayout
import androidx.fragment.app.Fragment
import com.example.shiwuyouproject.base.BaseVmActivity
import com.example.shiwuyouproject.databinding.ActivityMyinvitationBinding
import com.example.shiwuyouproject.ui.fragment.my.activity.adapter.ExamplePagerAdapter
import com.example.shiwuyouproject.ui.fragment.my.activity.fragment.MyInvitationFragment
import com.example.shiwuyouproject.ui.fragment.my.activity.viewmodel.MyInvitationViewModel
import net.lucode.hackware.magicindicator.ViewPagerHelper
import net.lucode.hackware.magicindicator.buildins.UIUtil
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.ColorTransitionPagerTitleView
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView


/**
 * 我的邀请
 */
class MyInvitationActivity : BaseVmActivity<ActivityMyinvitationBinding, MyInvitationViewModel>() {
    override fun viewModelClass(): Class<MyInvitationViewModel> = MyInvitationViewModel::class.java
    private val CHANNELS = arrayOf("最近分期", "最高分拥")
    private var mExamplePagerAdapter: ExamplePagerAdapter? = null
    override fun getViewBinding(): ActivityMyinvitationBinding {
        return ActivityMyinvitationBinding.inflate(layoutInflater)
    }

    private var mFgtList: MutableList<Fragment> = ArrayList()
    override fun initView() {
        mBinding.myTitleBar.tvTitle.text = "我的邀请"

    }

    override fun initData() {
        for (i in 0..1) {
            val mfgt = MyInvitationFragment()
            val bundle = Bundle()
            bundle.putString("type", (i + 1).toString())
            mfgt.arguments = bundle
            mFgtList.add(mfgt)
        }
        mExamplePagerAdapter = ExamplePagerAdapter(mFgtList, supportFragmentManager)
        mBinding.myViewPager2.adapter = mExamplePagerAdapter
        initMagicIndicator1()
    }

    override fun setListener() {
        mBinding.myTitleBar.tvBack.setOnClickListener { finish() }
    }

    override fun observe() {


    }

    @SuppressLint("UseCompatLoadingForDrawables")
    private fun initMagicIndicator1() {
        val commonNavigator = CommonNavigator(this)
        commonNavigator.adapter = object : CommonNavigatorAdapter() {
            override fun getCount(): Int {
                return CHANNELS.size
            }

            override fun getTitleView(context: Context, index: Int): IPagerTitleView {
                val simplePagerTitleView: SimplePagerTitleView =
                    ColorTransitionPagerTitleView(context)
                simplePagerTitleView.text = CHANNELS[index]
                val font = Typeface.create(Typeface.SANS_SERIF, Typeface.BOLD)
                simplePagerTitleView.typeface = font
                simplePagerTitleView.textSize = 16F
                simplePagerTitleView.normalColor = Color.parseColor("#929395")
                simplePagerTitleView.selectedColor = Color.parseColor("#5580FE")
                simplePagerTitleView.setOnClickListener {
                    mBinding.myViewPager2.currentItem = index
                }
                return simplePagerTitleView
            }

            override fun getIndicator(context: Context): IPagerIndicator {
                val indicator = LinePagerIndicator(context)
                indicator.setColors(Color.parseColor("#5580FE"))
                return indicator
            }
        }
        mBinding.magicIndicator1.navigator = commonNavigator
        val titleContainer = commonNavigator.titleContainer // must after setNavigator
        titleContainer.showDividers = LinearLayout.SHOW_DIVIDER_BEGINNING
        titleContainer.dividerPadding = UIUtil.dip2px(this, 80.0)
//        titleContainer.dividerDrawable = resources.getDrawable(R.drawable.simple_splitter)
        ViewPagerHelper.bind(mBinding.magicIndicator1, mBinding.myViewPager2)
    }
}